<?php

namespace App\Http\Requests\Api\Admin;

use App\Http\Requests\Api\FormRequest;
use App\Http\Business\CaptchasBusiness;

class AdminUserRequest extends FormRequest
{
    public function rules()
    {
        return [
            'name' => ['required', 'max:16', 'unique:admin_users,name'],
            'password' => ['required', 'max:16', 'min:6'],
            'phone' => ['required', 'regex:/^1[3456789]\d{9}$/', 'unique:admin_users,phone']
        ];
    }

    public function messages()
    {
        return [
            'user_id.required'=>'用户ID必须填写',
            'user_id.exists'=>'用户不存在',
            'name.unique' => '用户名已经存在',
            'phone.unique' => '手机号已经存在',
            'name.required' => '用户名不能为空',
            'name.max' => '用户名最大长度为16个字符',
            'password.required' => '密码不能为空',
            'password.max' => '密码长度不能超过16个字符',
            'password.min' => '密码长度不能小于6个字符',
            'phone.regex' => '手机号码格式错误'
        ];
    }

    /**
     * 验证后执行的操作
     * @param $validator
     */
    public function withValidator($validator)
    {
        $validator->after(function ($validator) {
            //var_dump($validator->errors());exit();
        });
        if ($validator->fails()){
            (new CaptchasBusiness())->setCaptcha();
        }
    }
}
